﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using DevExpress.ExpressApp.Templates;
using DevExpress.ExpressApp.Web;
using DevExpress.ExpressApp.Web.Templates;
using DevExpress.ExpressApp.Web.Templates.ActionContainers;
using FeatureCenter.Module.Web.InfoPanels;

namespace FeatureCenter.Web {
    public partial class DefaultVerticalTemplateContent : TemplateContent, DevExpress.ExpressApp.Demos.IHintTemplate, ICustomControlsPlaceHolder {
        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
            TRP.PreRender += new EventHandler(TRP_PreRender);
            if(WebWindow.CurrentRequestWindow != null) {
                WebWindow.CurrentRequestWindow.PagePreRender += new EventHandler(CurrentRequestWindow_PagePreRender);
            }
            ICallbackManagerHolder holder = Parent as ICallbackManagerHolder;
            if(holder != null) {
                holder.CallbackManager.CallbackControl.ClientSideEvents.EndCallback = "function(s, e){ UpdateRightPaneVisibility('Right', 'RS', 'RSI'); }";
            }
        }
        private void CurrentRequestWindow_PagePreRender(object sender, EventArgs e) {
            WebWindow window = (WebWindow)sender;
            window.RegisterStartupScript("OnLoadCore", string.Format(@"Init(""{0}"", ""VerticalCS"");OnLoadCore(""LPcell"", ""Right"", ""LS"",""RS"", ""LSI"",""RSI"");", BaseXafPage.CurrentTheme), true);
        }
        protected override void OnUnload(EventArgs e) {
            if(WebWindow.CurrentRequestWindow != null) {
                WebWindow.CurrentRequestWindow.PagePreRender -= new EventHandler(CurrentRequestWindow_PagePreRender);
            }
            TRP.PreRender -= new EventHandler(TRP_PreRender);
            base.OnUnload(e);
        }
        public override IActionContainer DefaultContainer {
            get {
                if(TB != null) {
                    return TB.FindActionContainerById("View");
                }
                return null;
            }
        }
        public override void SetStatus(ICollection<string> statusMessages) {
            InfoMessagesPanel.Text = string.Join("<br>", new List<string>(statusMessages).ToArray());
        }

        public override object ViewSiteControl {
            get { return VSC; }
        }

        #region ICustomControlsPlaceHolder Members
        public Control PlaceHolder {
            get { return UPRight; }
        }
        #endregion

        public ICollection<IActionContainer> GetContainers() {
            throw new NotImplementedException();
        }
        public void SetView(DevExpress.ExpressApp.View view) {
        }
        public string Hint {
            get {
                return hintPanel.Label.Text;
            }
            set {
                hintPanel.Label.Text = value;
                hintPanel.ClientVisible = !String.IsNullOrEmpty(value);
            }
        }
        public void UpdateToolsRoundPanelVisibility() {
            bool isVisible = false;
            foreach(Control control in TRP.Controls) {
                if(control is ActionContainerHolder) {
                    if(((ActionContainerHolder)control).HasActiveActions()) {
                        isVisible = true;
                        break;
                    }
                }
            }
            TRP.Visible = isVisible;
        }
        private void TRP_PreRender(object sender, EventArgs e) {
            UpdateToolsRoundPanelVisibility();
        }

    }
}
